;
(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.ticket, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.ticket, this._id);
		},
		getDetail: function() {
			return {
				ticket: this,
				bus: BusCon.get(this.bus_id)
			}
		},
		setInfo: function(info) {
			//已经使用的次数
			this.info.use_times = ~~info.use_times;
			this.info.use_achs = Array.isArray(info.use_achs) ? info.use_achs : [];
		},
		getInfo: function() {
			var info = this.info;
			// info.use_times = ~~info.use_times;
			info.use_achs = Array.isArray(info.use_achs) ? info.use_achs : [];
			return info;
		},
		toBuff: function() {
			var buff_id = this.buff_id;
			var buff;
			//无BUFF或者BUFF无效时，重新生成
			if (!buff_id || !(buff = BuffCon.get(buff_id))) {
				var buff = new BuffCon({
					name: this.info.name,
					code_id: "优惠券",
					config: {
						ticket_id: this._id
					}
				});
				buff_id = this.buff_id = buff._id;
				this.save();
			}
			return buff;
		},
		//代码片段，判断这个优惠券可否用于某个订单
		_canUseForAch: function(ach) {
			return TicketFactoryCon.canUseForAch(this.ticket_factory, ach);
		},
		//代码片段，使用
		_useForAch: function(ach_id) {
			this.info.use_times += 1;
			if (this.info.use_times >= this.ticket_factory.info.times) {
				this.state = TicketCon.STATE.已使用;
			}
			this.getInfo().use_achs.push(ach_id);
			this.save();
		},
		//代码片段，回滚使用
		_unUseForAch: function(ach_id) {
			this.info.use_times > 0 && (this.info.use_times -= 1);
			if (this.info.use_times >= this.ticket_factory.info.times) {
				this.state = TicketCon.STATE.已使用;
			} else {
				this.state = TicketCon.STATE.未使用
			}
			this.getInfo().use_achs = $Array.remove(this.info.use_achs, ach_id);
			this.save();
		},
	};
	module.exports = skill;

	var db = require("../db/index");
	var TicketCon = require("../ticket/_con");
	var TicketFactoryCon = require("../ticket_factory/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());